package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MD5Util;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.dbutils.SpUtil;
import com.kpx.app.utils.encrypt.Toolkit;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.ClearEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/3.
 * 设置提现密码
 */

public class ActivityResetTxPwd extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.txt_1)
    TextView txt1;
    @BindView(R.id.txt_2)
    TextView txt2;
    @BindView(R.id.cet_phone)
    ClearEditText cetPhone;
    @BindView(R.id.txt_3)
    TextView txt3;
    @BindView(R.id.btn_code)
    Button btnCode;
    @BindView(R.id.cet_code)
    ClearEditText cetCode;
    @BindView(R.id.txt_4)
    TextView txt4;
    @BindView(R.id.btn_save)
    Button btnSave;
    @BindView(R.id.cet_pwd)
    ClearEditText cetPwd;
    @BindView(R.id.cet_pwd_s)
    ClearEditText cetPwdS;
    private int time = 120;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_reset_tx_pwd);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "钱包密码");
        cetPhone.setText(UserInfoUtils.getCachedLoginUser().getUSER().getPhone());
        cetPhone.setEnabled(false);
        cetPhone.setClearIconVisible(false);
    }

    @Override
    public void initData() {

    }
    @Override
    protected void onResume() {
        super.onResume();
        long lasttime = SpUtil.getLong(Constants.TIME_CODE_4);
        if(lasttime!=0&&System.currentTimeMillis()-lasttime<120*1000){
            btnCode.setEnabled(false);
            time = (int)((120*1000-System.currentTimeMillis()+lasttime)/1000);
            handler.sendEmptyMessage(0);
        }
    }
    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.btn_code, R.id.btn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                if (TextUtils.isEmpty(cetPhone.getText().toString().trim())) {
                    ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "请输入电话号码");
                    return;
                }
                toGetCode();
                break;
            case R.id.btn_save:
                toSave();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            time--;
            if (time <= 0) {
                btnCode.setEnabled(true);
                btnCode.setText("获取验证码");
            } else {
                btnCode.setText(time + "s");
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

    private void toGetCode() {
        showLoadingDialog();
        UserInfoUtils.getCode(ActivityResetTxPwd.this, cetPhone.getText().toString().trim(), 4, new UserInfoUtils.OnGetCodeBackListener() {
            @Override
            public void back(boolean s) {
                dissmissLoadingDialog();
                if (s) {
                    time = 120;
                    btnCode.setEnabled(false);
                    handler.sendEmptyMessage(0);
                    ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "获取成功");
                    SpUtil.putLong(Constants.TIME_CODE_4,System.currentTimeMillis());
                }
            }
        });
    }

    /**
     * 参数：token,String pwd, String authCode
     */
    private void toSave() {
        if (TextUtils.isEmpty(cetPwd.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "请输入您的密码");
            return;
        }
        if (TextUtils.isEmpty(cetPwdS.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "请输入您的确认密码");
            return;
        }
        if (!cetPwdS.getText().toString().trim().equals(cetPwd.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "两次密码不一样");
            return;
        }
        if (TextUtils.isEmpty(cetCode.getText().toString().trim())) {
            ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "请输入您的验证码");
            return;
        }
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("pwd", MD5Util.encrypt(cetPwd.getText().toString().trim()));
        map.put("authCode", cetCode.getText().toString().trim());
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                    if (loginUser != null) {
                        loginUser.getWALLET().setHasPwd(1);
                        UserInfoUtils.saveLoginUser(loginUser);
                    }
                    ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, "提现密码添加完成");
                    finish();
                } else {
                    ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityResetTxPwd.this, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_GET_WALLET_SET_PWD, true, map);
    }
}
